<?php

namespace backend\modules\system\controllers;

use backend\extensions\ApiBaseController;
use backend\services\system\DictCateService;
use backend\services\system\DictItemService;

class DictController extends ApiBaseController {
    
    /**
     * 角色列表
     * * */
    public function actionCateList() {
        $params = \Yii::$app->request->post();
        $rules = [
            [['name','code'], 'default', 'value' => 0, 'message' => '检索条件'],
            [['page'], 'default', 'value' => 1, 'message' => '页数'],
            [['page_size'], 'default', 'value' => 30, 'message' => '每页显示总数'],
        ];
        $validate = $this->validateParams($params, $rules);
        if ($validate) {
            return $this->jsonError($validate);
        }
        $data = DictCateService::getList($this->params);
        return $this->jsonSuccess($data);
    }
    
    /**
     * 字典分类保存
     * * */
    public function actionCateCreate() {
        $params = \Yii::$app->request->post();
        $rules = [
            [['name','code'], 'required', 'message' => '{attribute}属必填项'],
            [['id','desc'], 'default', 'value' => '', 'message' => 'ID'],
        ];
        $validate = $this->validateParams($params, $rules);
        if ($validate) {
            return $this->jsonError($validate);
        }
        if($this->params['id']){
            return DictCateService::update($this->params);
        }
        unset($this->params['id']);
        return DictCateService::create($this->params);
    }
    
    /**
     * 字典分类删除
     * * */
    public function actionCateDel() {
        $params = \Yii::$app->request->post();
        $rules = [
            [['id'], 'required', 'message' => '{attribute}属必填项'],
        ];
        $validate = $this->validateParams($params, $rules);
        if ($validate) {
            return $this->jsonError($validate);
        }
        return DictCateService::delete($this->params);
    }
    
    /**
     * 字典项列表
     * * */
    public function actionItemList() {
        $params = \Yii::$app->request->post();
        $rules = [
            [['cate_id'], 'default', 'value' => 0, 'message' => '字典分类ID'],
            [['name'], 'default', 'value' => 0, 'message' => '名称检索'],
        ];
        $validate = $this->validateParams($params, $rules);
        if ($validate) {
            return $this->jsonError($validate);
        }
        $data = DictItemService::getList($this->params);
        return $this->jsonSuccess($data);
    }
    
    /**
     * 字典项保存
     * * */
    public function actionItemCreate() {
        $params = \Yii::$app->request->post();
        $rules = [
            [['cate_id','name','value'], 'required', 'message' => '{attribute}属必填项'],
            [['id','desc'], 'default', 'value' => '', 'message' => 'ID'],
        ];
        $validate = $this->validateParams($params, $rules);
        if ($validate) {
            return $this->jsonError($validate);
        }
        if($this->params['id']){
            return DictItemService::update($this->params);
        }
        unset($this->params['id']);
        return DictItemService::create($this->params);
    }
    
    /**
     * 字典项删除
     * * */
    public function actionItemDel() {
        $params = \Yii::$app->request->post();
        $rules = [
            [['id'], 'required', 'message' => '{attribute}属必填项'],
        ];
        $validate = $this->validateParams($params, $rules);
        if ($validate) {
            return $this->jsonError($validate);
        }
        return DictItemService::delete($this->params);
    }

    

}
